package com.wuba.rxjavademo.lyz.day0429;

import com.wuba.rxjavademo.lyz.BaseDemo;

import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.schedulers.Schedulers;

/**
 * Created by liyaxing on 2016/5/19.
 */
public class Demo05 extends BaseDemo{

    public static void main(String[] ss){
        Observable<Observable<Integer>> observableObservable =
                Observable.create(new Observable.OnSubscribe<Observable<Integer>>() {
                    @Override
                    public void call(Subscriber<? super Observable<Integer>> subscriber) {
                        for (int i = 0; i < 10; i++) {
                            subscriber.onNext(createObservable(i));
                        }
                        subscriber.onCompleted();
                    }
                }) ;
        Observable.merge(observableObservable).toList().subscribe(new Subscriber<List<Integer>>() {
            @Override
            public void onCompleted() {
                print("--onCompleted--");
            }
            @Override
            public void onError(Throwable e) {

            }
            @Override
            public void onNext(List<Integer> integers) {
                print("thread = " + Thread.currentThread().getId()) ;
                print("--onNext-- integers = " + integers);
            }
        }) ;
        try {
            Thread.sleep(500000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static Observable<Integer> createObservable(int value){
        return Observable.create(new Observable.OnSubscribe<Integer>() {
            @Override
            public void call(Subscriber<? super Integer> subscriber) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                subscriber.onNext(value);
                subscriber.onCompleted();
            }
        }).subscribeOn(Schedulers.io()) ;
    }

}
